package Controller

import (
	"back_go/DAO"
	models "back_go/DAO/model"
	"back_go/Response"
	"github.com/gin-gonic/gin"
	"math"
	"net/http"
	"strconv"
	"time"
)

// SysLogPage 系统日志分页查询
/*
   参数：
   	current: 当前页码，默认为1
 	size: 每页数量，默认为10
    return: 系统日志分页查询
*/
func SysLogPage(c *gin.Context) {
	var engine = DAO.Engine
	currentStr := c.Query("current")
	sizeStr := c.Query("size")
	page, err := strconv.Atoi(currentStr)
	if err != nil || page < 1 {
		page = 1
	}

	size, err := strconv.Atoi(sizeStr)
	if err != nil || size < 1 {
		size = 10
	}
	offset := (page - 1) * size
	var logs []models.Logs
	var log models.Logs
	err = engine.Desc("timestamp").Limit(size, offset).Find(&logs)
	count, _ := engine.Count(&log)
	current, _ := strconv.Atoi(currentStr)
	size, _ = strconv.Atoi(sizeStr)

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code: "00000",
		Data: PageParam1[models.Logs]{
			Current: current,
			Total:   count,
			Size:    size,
			Records: logs,
			Pages:   int(math.Ceil(float64(count) / float64(size))),
		},
		Msg:       nil,
		Success:   true,
		Fail:      false,
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "",
	})

}
